<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Dashboard extends CI_Controller {

	var $debug = '';
	var $per_page = '';

	public function __construct()
	{
		parent::__construct();	

		$logged_in = $this->session->userdata('logged_in');
		if($logged_in)
		{
		   $session_data = $this->session->userdata('logged_in');
		   $data['username'] = $session_data['username'];		   
		}
		else
		{
	     //If no session, redirect to login page
	     redirect('login', 'refresh');
   		}

   		// per page limit 10 items
   		$this->per_page = 10;

		// gán giá trị tiêu đề form
		$this->template->set('nav',$this->uri->segment(1));
		$this->template->set('title', 'Bảng điều khiển');
		$this->template->set('section', 'Tổng số đơn hàng (Nhập kho/xuất kho)');

		$this->load->model('Dashboard_model');

		// load model storage
		$this->load->model('Storage_model');
		$this->load->model('Cron_model');
		$this->load->model('Product_model');
	}		

	public function index($start = false)	
	{
		$warning = $this->session->userdata('warning');

		$notify_product = $this->session->userdata('notify_product');		

		if(!empty($notify_product))
			$data['notify_product'] = $notify_product;

		$notify_search = $this->session->userdata('notify_search');
		
		if(!empty($notify_search))
		{
			$data['search'] = $notify_search;	
			$this->session->unset_userdata('notify_search');
		}

		$cond_search = array();
		if(!empty($data['search']))
		{			
			$cond_search['code'] = $data['search'];
			// fix special character 
			$cond_search['code'] = str_replace("'", "\'", $cond_search['code']);
			// remove accents		
			$slug = $this->accents->removeaccents($data['search']);
			$cond_search['slug'] = url_title($slug);
		}	

		// calculate total import & export
		$data['totalAllImport'] = $this->Dashboard_model->countAll(1);
		$data['totalAllExport'] = $this->Dashboard_model->countAll(2);		

		// load storage status info
		$data['storageInfo'] = $this->Storage_model->loadStorageStatInfo();

		// load backup system report
		$data['backup'] = $this->Cron_model->getLastUpdatedCron();

		if(!empty($warning))
		{
			$data['warning'] = $warning;
			$this->session->unset_userdata('warning');
		}		

		$this->load->library('pagination');
		/////////////// Enable paging /////////////////////////
		if(empty($start))
			$start = 1;

		$start = ($start - 1) * $this->per_page;
		$limit = 10;

		// load all products
		$products = $this->Product_model->loadAllV3($start, $limit, $cond_search);

		$data['products'] = $products['data'];
		$count_products = $products['count'];
		$data['total'] = $count_products;

		////////////////// pagination //////////////////////////////////////
		$config['base_url'] = site_url('dashboard/index/');
		$config['total_rows'] = $count_products;
		$config['per_page'] = $this->per_page;
		$config['uri_segment'] = 3;
		$config['use_page_numbers'] = TRUE;
		$config['full_tag_open'] = '<ul class="pagination">';
		$config['full_tag_close'] = '</ul>';
		// first link
		$config['first_link'] = '&laquo;';
		$config['first_tag_open'] = '<li>';
		$config['first_tag_close'] = '</li>';
		// next link
		$config['next_tag_open'] = '<li>';
		$config['next_tag_close'] = '</li>';	
		// previous link
		$config['prev_tag_open'] = '<li>';
		$config['prev_tag_close'] = '</li>';
		// end link
		$config['last_link'] = '&raquo;';
		$config['last_tag_open'] = '<li>';
		$config['last_tag_close'] = '</li>';
		// current
		$config['cur_tag_open'] = '<li class="active"><a href="#">';
		$config['cur_tag_close'] = '<span class="sr-only">(current)</span></a></li>';
		// digit link
		$config['num_tag_open'] = '<li>';
		$config['num_tag_close'] = '</li>';

		$this->pagination->initialize($config); 

		$data['paging_products'] = $this->pagination->create_links();

		////////////////////////////////////////////////////////////////////

		
		//////////////   Finish Prducts ///////////////////////

		$this->template->load('admin/template','admin/dashboard',$data);
	}

	public function search()
	{
		if(!empty($warning))
		{
			$data['warning'] = $warning;
			$this->session->unset_userdata('warning');
		}

		$notify_product = $this->session->userdata('notify_product');

		if(!empty($notify_product))
			$data['notify_product'] = $notify_product;

		$fromDate = $this->input->post('fromDate');
		$toDate = $this->input->post('toDate');

		// calculate total import & export
		$data['totalAllImport'] = $this->Dashboard_model->countAll(1);
		$data['totalAllExport'] = $this->Dashboard_model->countAll(2);

		// load storage status info
		$data['storageInfo'] = $this->Storage_model->loadStorageStatInfo();

		// load backup system report
		$data['backup'] = $this->Cron_model->getLastUpdatedCron();

		// search if available
		$search = $this->input->post('search');
		$notify_search = $this->session->userdata('notify_search');
		$cond_search = array();
		if(!empty($search))
		{
			$data['search'] = $search;			
			$this->session->set_userdata('search', $data['search']);
		}
		else {
			$this->session->set_userdata('search', '');	
		}
		$session_search = $this->session->userdata('search');
		if(!empty($session_search))
		{
			$data['search'] = $session_search;
		}
		if(!empty($notify_search))
		{
			$data['search'] = $notify_search;
			$this->session->unset_userdata('notify_search');
		}

		if(!empty($data['search']))
		{			
			$cond_search['code'] = $data['search'];
			// fix special character 
			$cond_search['code'] = str_replace("'", "\'", $cond_search['code']);
			// remove accents		
			$slug = $this->accents->removeaccents($data['search']);
			$cond_search['slug'] = url_title($slug);
		}

		$this->load->library('pagination');
		/////////////// Enable paging /////////////////////////
		if(empty($start))
			$start = 1;

		$start = ($start - 1) * $this->per_page;
		$limit = 10;

		// load all products
		$products = $this->Product_model->loadAllV3($start, $limit, $cond_search);

		$data['products'] = $products['data'];
		$count_products = $products['count'];
		$data['total'] = $count_products;

		////////////////// pagination //////////////////////////////////////
		$config['base_url'] = site_url('dashboard/index/');
		$config['total_rows'] = $count_products;
		$config['per_page'] = $this->per_page;
		$config['uri_segment'] = 3;
		$config['use_page_numbers'] = TRUE;
		$config['full_tag_open'] = '<ul class="pagination">';
		$config['full_tag_close'] = '</ul>';
		// first link
		$config['first_link'] = '&laquo;';
		$config['first_tag_open'] = '<li>';
		$config['first_tag_close'] = '</li>';
		// next link
		$config['next_tag_open'] = '<li>';
		$config['next_tag_close'] = '</li>';	
		// previous link
		$config['prev_tag_open'] = '<li>';
		$config['prev_tag_close'] = '</li>';
		// end link
		$config['last_link'] = '&raquo;';
		$config['last_tag_open'] = '<li>';
		$config['last_tag_close'] = '</li>';
		// current
		$config['cur_tag_open'] = '<li class="active"><a href="#">';
		$config['cur_tag_close'] = '<span class="sr-only">(current)</span></a></li>';
		// digit link
		$config['num_tag_open'] = '<li>';
		$config['num_tag_close'] = '</li>';

		$this->pagination->initialize($config); 

		$data['paging_products'] = $this->pagination->create_links();

		////////////////////////////////////////////////////////////////////
		
		if(!empty($fromDate) && !empty($toDate))
		{			
			// format date
			$fromDate = date('Y-m-d',strtotime($fromDate));
			$toDate = date('Y-m-d', strtotime('+1 day',strtotime($toDate)));

			$data['fromDate'] = $fromDate;
			$data['toDate'] = $toDate;

			if(strtotime($fromDate) > strtotime($toDate))
			{
				$this->session->set_userdata('warning','Đã có lỗi xảy ra. Xin vui lòng kiểm tra ngày nhập !');
				redirect('dashboard/index');
			}

			// count import product, export
			$importData = array();
			$importCount = $this->Dashboard_model->getImportHistory($fromDate, $toDate);

			foreach ($importCount as $row) 
			{
				$importData[$row->day] = $row->import;
			}		

			// export
			$exportData = array();
			$exportCount = $this->Dashboard_model->getExportHistory($fromDate, $toDate);

			foreach ($exportCount as $row) 
			{
				$exportData[$row->day] = $row->export;
			}		

			// 
			$data['totalImport'] = 0;
			$data['totalExport'] = 0;

			$showData = array();
			$nextDate = $fromDate;
			while (strtotime($nextDate) <= strtotime($toDate)) {
				$showData[$nextDate]['import'] = isset($importData[$nextDate]) ? $importData[$nextDate] : 0;
				$showData[$nextDate]['export'] = isset($exportData[$nextDate]) ? $exportData[$nextDate] : 0;									

				$data['totalExport'] += $showData[$nextDate]['export'];
				$data['totalImport'] += $showData[$nextDate]['import'];

				$nextDate = date('Y-m-d', strtotime("+1 day", strtotime($nextDate)));						
			}		

			if(!empty($showData))
			{
				$data['showData'] = $showData;		
			}			
		}	
		else
		{
			$this->session->set_userdata('warning','Đã có lỗi xảy ra. Xin vui lòng kiểm tra ngày nhập !');			
		}		

		$this->template->load('admin/template','admin/dashboard',$data);
	}
}	